﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.Valuables
{
    public partial class valuablesdetail : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZSBGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("student");
            strPageUserType.Add("teacher");

            if (Request.QueryString["ID"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["ID"].ToString();
            if (!Page.IsPostBack)
            {
                ExtendModule.InstrumentAndConsumables mvaluables = checkQueryString(strID);
                if (mvaluables == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "字典类别不存在!");
                    return;
                }
                bindDDL(mvaluables);
                Bind(mvaluables);
                ddlArrow.Enabled = false;
                ddlFundsSubject.Enabled = false;
                ddlPresentSituation.Enabled = false;
                ddlSource.Enabled = false;
            }
        }

        ExtendModule.InstrumentAndConsumables checkQueryString(string strID)
        {
            ExtendBLL.InstrumentAndConsumables bvaluables = new ExtendBLL.InstrumentAndConsumables();
            string strWhere = " Type = 'guizhongyiqi' and ID =" + strID;
            List<ExtendModule.InstrumentAndConsumables> listvaluables = bvaluables.GetExtendModelList(strWhere);
            if (listvaluables.Count <= 0)
            {
                return null;
            }
            return listvaluables[0];
        }

        public void bindDDL(ExtendModule.InstrumentAndConsumables mvaluables)
        {
            tbCreatedDate.Text = DateTime.Today.ToShortDateString();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlSource.DataSource = listCClass;
            ddlSource.DataTextField = "Name";
            ddlSource.DataValueField = "Code";
            ddlSource.DataBind();
            ddlSource.SelectedValue = mvaluables.Source;

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();
            ddlArrow.SelectedValue = mvaluables.Arrow;

            listCClass = eDictionary.GetItemListByDClass("shebeizhuangtai");
            ddlPresentSituation.DataSource = listCClass;
            ddlPresentSituation.DataTextField = "Name";
            ddlPresentSituation.DataValueField = "Code";
            ddlPresentSituation.DataBind();
            ddlPresentSituation.SelectedValue = mvaluables.ResponesoblePerson;

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();
            ddlFundsSubject.SelectedValue = mvaluables.FundsSubject;

            tbLocation.Text = mvaluables.LabName;
        }

        public void Bind(ExtendModule.InstrumentAndConsumables mvaluables)
        {
            EquipPhoto.ImageUrl = "~/Equipment/PicShow.aspx?ID=" + mvaluables.ID;
            tbInstrumentNum.Text = mvaluables.Numbers;
            tbKindNum.Text = mvaluables.ClassNumber;
            tbInstrumentName.Text = mvaluables.Name;
            tbStandards.Text = mvaluables.Standards;
            tbModel.Text = mvaluables.Model;
            tbCountryCode.Text = mvaluables.CountryCode;
            tbAmount.Text = mvaluables.Quantity.ToString();
            tbUnitPrice.Text = mvaluables.UnitPrice;
            tbFactoryCode.Text = mvaluables.FactoryCode;
            tbDocuments.Text = mvaluables.Documents;
            tbMeasurement.Text = mvaluables.Measurement;
            try
            {
                tbProductionDate.Text = mvaluables.ProductionDate.Value.ToShortDateString();
                tbPurchaseDate.Text = mvaluables.PurchaseDate.Value.ToShortDateString();
                tbOpeningTime.Text = mvaluables.OpeningTime.Value.ToShortDateString();
                tbCreatedDate.Text = mvaluables.CreatedDate.Value.ToShortDateString();
            }
            catch { }

            tbResponesoblePerson.Text = mvaluables.ResponesoblePerson;
            tbContactPhone.Text = mvaluables.ContactPhone;
            tbCharges.Text = mvaluables.Charges;
            tbZipCode.Text = mvaluables.ZipCode;
            tbMailingAddress.Text = mvaluables.MailingAddress;
            tbPurpose.Text = mvaluables.Purpose;
        }

        public void btDelete_Click(object sender, EventArgs e)
        {
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            try
            {
                bconsum.Delete(long.Parse(strID));
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "DeleteSuccess", "删除成功!", "valuablespreview.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }

        }
    }
}
